$(function () {
    $("body").on('click','[data-stopPropagation]',function (e) {e.stopPropagation();});
    /**意向等级**/
    $(".multiple-select2").select2({placeholder:"选择意向客户"});
    $(".multiple-select2").on("change", function(e) {
        var yixiang = $("#bpGroud").val();
        var yArray = new Array();
        $.each(yixiang,function (index,val) {yArray.push(val);});
        var conver = yArray.join(",");
        $("#bpgroupSelect").val(conver);
        $JuXue.refreshTable("bigDisTable");
    });
    /**导入批次**/
    $.getJSON(ctx+"bigEpExport/getlimitBigRecords",{},function (r) {
        var recodsArray = new Set();
        $.each(r,function (index,value) {
            recodsArray.add(value.epBatch);
        });
        $("#bpBatch").autocomplete({
            source: Array.from(recodsArray),
            select: function( event, ui ) {
                $("#bpBatch").val(ui.item.label);
                $JuXue.refreshTable("bigDisTable");
            }
        });
    });

    /**是否分配*/
    $(".multiple-select5").select2({placeholder:"选择是否分配"});
    $(".multiple-select5").on("change", function(e) {
        var distruteNum = $("#distruteNum").val();
        $("#distrutSelect").val(distruteNum);
        $JuXue.refreshTable("bigDisTable");
    });

    /**部门*/
    $(".multiple-select6").select2({placeholder:"选择所属部门"});
    $.getJSON(ctx+"dept/list",{},function (r) {
        $("#bpDeptId").empty().append("<option value=\"\">请选择部门</option>").append("<option value=\"\"></option>");
        $.each(r,function (index,value) {
            var deptid = value.deptId;
            var deptname = value.deptName;
            $("#bpDeptId").append("<option value="+deptid+">"+deptname+"</option>");
        })
    });
    $(".multiple-select6").on("change", function(e) {
        var bpDeptId = $("#bpDeptId").val();
        $("#deptHid").val(bpDeptId);
        $JuXue.refreshTable("bigDisTable");
        changeDirecter(bpDeptId);
    });


    $(".multiple-select7").select2({placeholder:"选择跟进人"});
    $(".multiple-select7").on("change", function(e) {
        var bpDistribute = $("#bpDistribute").val();
        $("#distributeHid").val(bpDistribute);
        $JuXue.refreshTable("bigDisTable");
    });

    $("#advance-daterange span").html(moment().subtract("days",29).format("YYYY-MM-DD")+" - "+moment().format("YYYY-MM-DD"));

    $('#advance-daterange').daterangepicker({
        timePicker: true, //显示时间
        timePicker24Hour: true, //时间制
        timePickerSeconds: true, //时间显示到秒
        startDate: moment().hours(0).minutes(0).seconds(0), //设置开始日期
        endDate: moment(new Date()), //设置结束器日期
        maxDate: moment(new Date()), //设置最大日期
        "opens": "center",
        ranges: {
            '今天': [moment(), moment()],'昨天': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
            '上周': [moment().subtract(6, 'days'), moment()],
            '前30天': [moment().subtract(29, 'days'), moment()],
            '本月': [moment().startOf('month'), moment().endOf('month')],
            '上月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
        },
        showWeekNumbers: true,
        locale: {
            format: "YYYY-MM-DD HH:mm:ss", //设置显示格式
            applyLabel: '确定', //确定按钮文本
            cancelLabel: '取消', //取消按钮文本
            customRangeLabel: '自定义',
            daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
            monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
                '七月', '八月', '九月', '十月', '十一月', '十二月'
            ],
            firstDay: 1
        },
    }).on('apply.daterangepicker', function(ev, picker) {
        var starttime = picker.startDate.format('YYYY-MM-DD');
        var endtime = picker.endDate.format('YYYY-MM-DD');
        $("#advance-daterange span").html(starttime+" - "+endtime);
        $("#starttime").val(starttime);
        $("#endtime").val(endtime);
        $JuXue.refreshTable("bigDisTable");
    });
    /**ai资源批次的输入**/
    $('#bpBatch').live('input propertychange', function() {
        $JuXue.refreshTable("bigDisTable");
    });
    /**所属专员的输入**/
    $('#bpdirector').live('input propertychange', function() {
        $JuXue.refreshTable("bigDisTable");
    });
    init_cusSale();
});
function changeDirecter(deptid) {
    $.getJSON(ctx+"user/selectUserByDept",{deptId:deptid},function (r) {
        $("#bpDistribute").empty();
        $.each(r,function (index,val) {
            var userUuid = val.userUuid;
            var nickname = val.nickname;
            $("#bpDistribute").append("<option value="+userUuid+">"+nickname+"</option>");
        })
    })
}

/**初始化客户**/
function init_cusSale(){
    var settings = {
        url: ctx + "aiEpResoure/list",
        pageSize: 15,
        queryParams: function(params) {
            return {
                pageSize: params.limit,
                pageNum: params.offset / params.limit + 1,
                //筛选条件
                aiBatch:  $("#bpBatch").val(),//批次
                aiDirector :$("#bpdirector").val(),//所属专员
                aiGroud  :$("#bpgroupSelect").val(), //意向等级
                aiResType:$("#bpResType").val(), //大资源类型
                starttime :$("#starttime").val(),     //考试时间
                endtime :$("#endtime").val(),         //结束时间
                isBig : $("#distrutSelect").val(), //是否分配
                aiZone:$("#aiZoneSelect").val(), //选择地区
                aiDeptid :$("#deptHid").val(), //部门
                aiDistriute :$("#distributeHid").val() //跟进人
            };
        },
        columns: [{
            checkbox: true
        },
            {
                field: 'aiName',
                title: '客户名称',
                width: 150,
                clickToSelect:false,
                class:'text-center',
                formatter:function (value, row, index) {
                    return "<a href='javascript:void(0)' onclick='getCustomerDetail(this)' data-id='"+row.aiId+"'>"+value+"</a>";
                }
            }, {
                field: 'aiMobile',
                class:'text-center',
                title: '客户手机号'
            }, {
                field: 'aiGroud',
                class:'text-center',
                title: '意向等级'
            }, {
                field: 'aiZone',
                class:'text-center',
                title: '所属地区'
            }, {
                field: 'aiDistriute',
                class:'text-center',
                title: '是否分配',
                formatter:function (value, row, index) {
                    if(value==null||value==""){
                        return "否";
                    }else{
                        return "是";
                    }
                }
            }, {
                field: 'aiDeptName',
                class:'text-center',
                title: '当前所属部门'
            }, {
                field: 'aiDistriute',
                class:'text-center',
                title: '当前跟进人'
            }, {
                field: 'aiCreatetime',
                class:'text-center',
                title: '创建时间',
                formatter:function (value, row, index) {
                    return value.substring(0,10);
                }
            }, {
                field: 'aiDirector',
                class:'text-center',
                title: '创建人'
            }
        ],
        onClickCell:function(field, value, row, $element){
            if(field == "aiName"){return ;}else{
                $(".theme-panel").addClass("active");
                $("#selectTheme").show();
                var selected = $("#bigDisTable").bootstrapTable('getSelections');
                var selected_length = selected.length;
                $("#sutomeSelect").text(selected_length+1);
            }
        },
        onCheck:function (row,ele) {
            var selected = $("#bigDisTable").bootstrapTable('getSelections');
            var selected_length = selected.length;
            if (!selected_length) {
                $(".theme-panel").removeClass("active");
                $("#selectTheme").hide();
            }else{
                $(".theme-panel").addClass("active");
                $("#selectTheme").show();
            }
            $("#sutomeSelect").text(selected_length);
        },
        onUncheck:function (rows) {
            var selected = $("#bigDisTable").bootstrapTable('getSelections');
            var selected_length = selected.length;
            if (!selected_length) {
                $(".theme-panel").removeClass("active");
                $("#selectTheme").hide();
            }else{
                $(".theme-panel").addClass("active");
                $("#selectTheme").show();
            }
            $("#sutomeSelect").text(selected_length);
        },
        onCheckAll:function (rows) {
            $(".theme-panel").addClass("active");
            $("#selectTheme").show();
            $("#sutomeSelect").text(rows.length);
        },
        onUncheckAll:function (rows) {
            $(".theme-panel").removeClass("active");
            $("#selectTheme").hide();
        }
    };
    $JuXue.initTable('bigDisTable', settings);

}